﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace wpfInércia
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindowInercia : Window
    {
        public MainWindowInercia()
        {
            InitializeComponent();
        }
        Point ptVeio = new Point(0, 0);
        Point pt = new Point(0, 0);
        Size velo = new Size(0, 0);
        float inercia = .98F;
        float gravidade = .5F;
        Brush[] m_cores = new Brush[] {Brushes.Black, Brushes.Red, Brushes.Green, Brushes.Blue, Brushes.Yellow };
        List<Bola> m_bolas = new List<Bola>();
        private System.Windows.Threading.DispatcherTimer timer1;

        AnimationClock m_clock;
        RotateTransform m_rotateEllipse;


        private void button2_Click(object sender, RoutedEventArgs e)
        {


            //label1.BeginAnimation(label1,  ((Storyboard)App.Current.Resources["Storyboard1"]));
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            timer1 = new System.Windows.Threading.DispatcherTimer();
            timer1.Interval = TimeSpan.FromMilliseconds(50);
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
            RotateTransform rtf = new RotateTransform(3600, 20,20);
            ellipse1.RenderTransform = rtf;
            DoubleAnimation myAnimation =
            new DoubleAnimation(
                1, // "From" value
                1200, // "To" value 
                new Duration(TimeSpan.FromSeconds(4))
            );
            myAnimation.AutoReverse = true;
            myAnimation.DecelerationRatio = .3;
            AnimationClock clock = myAnimation.CreateClock();
            rtf.ApplyAnimationClock(RotateTransform.AngleProperty, clock);

        }

        void timer1_Tick(object sender, EventArgs e)
        {
            label1.Content = string.Format("{0:hh:mm:ss,ff}", DateTime.Now);
            List<Point> lstPoints = new List<Point>();
            foreach (Bola b in m_bolas)
            {
                Shape ctr = b.ctr as Shape;
                Random rd = new Random(DateTime.Now.Millisecond);
                double X = Canvas.GetLeft(ctr); //RenderedGeometry.Transform. GetRenderBounds(new Pen(Brushes.Black, 1F));                
                double Y = Canvas.GetTop(ctr);
                Bola hmm =  m_bolas.Find( delegate (Bola bl) 
                {
                    Shape ctl = bl.ctr as Shape;
                    if (ctr.Name == ctl.Name)
                        return false;
                    double a = Canvas.GetLeft(ctl);                
                    double c = Canvas.GetTop(ctl);
                    a = Math.Abs(Math.Abs(a) - Math.Abs( X));
                    c = Math.Abs(Math.Abs(c) - Math.Abs( Y));

                    return a < 10 && c < 10 ;
                });
                if ( hmm != null)
                {
                    b.velo.X = -1 * Math.Sign(b.velo.Y) * rd.Next(1, 10);
                    b.velo.Y = -1 * Math.Sign(b.velo.Y) * rd.Next(1, 10);
                    hmm.velo.X = -1 * Math.Sign(hmm.velo.Y) * rd.Next(1, 10);
                    hmm.velo.Y = -1 * Math.Sign(hmm.velo.Y) * rd.Next(1, 10);
                    if ((hmm.ctr as Shape).Name == "ellipse1")
                    {
                        m_rotateEllipse = new RotateTransform(0, 20,20);
                        (hmm.ctr as Shape).RenderTransform = m_rotateEllipse;
                        DoubleAnimation myAnimation =
                        new DoubleAnimation(
                            1, // "From" value
                            1200, // "To" value 
                            new Duration(TimeSpan.FromSeconds(2))
                        );
                        myAnimation.AutoReverse = true;
                        myAnimation.DecelerationRatio = .2;
                        myAnimation.AccelerationRatio = .2;
                        m_clock = myAnimation.CreateClock();
                        m_rotateEllipse.ApplyAnimationClock(RotateTransform.AngleProperty, m_clock);
                        ahh.Stop();
                        ahh.Play();
                    }
                }
                if ( X < 1)
                {
                    b.velo.X = rd.Next(1, 10);
                    b.velo.Y = Math.Sign(b.velo.Y) * rd.Next(1, 10);
                }
                if (X > (canvas1.ActualWidth - 20) )
                {
                    
                    b.velo.X = -1 * rd.Next(1,10);
                    b.velo.Y = Math.Sign(b.velo.Y) * rd.Next(1, 10);
                }
                if ( Y < 1)
                {
                    b.velo.X = Math.Sign(b.velo.X) * rd.Next(1, 10);
                    b.velo.Y = rd.Next(1, 10);
                }
                if (Y > (canvas1.ActualHeight - 20) )
                {
                    b.velo.X = Math.Sign(b.velo.X) * rd.Next(1, 10);
                    b.velo.Y = -1 * rd.Next(1, 10);
                }
                Canvas.SetLeft(ctr, X + b.velo.X);
                Canvas.SetTop(ctr, Y + b.velo.Y);
                lstPoints.Add(new Point(X,Y));
            }

        }

        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                
                Rectangle rec = new Rectangle();
                rec.Width = 20;
                rec.Height = 20;
                rec.RenderTransform = new TranslateTransform(e.GetPosition(canvas1).X, e.GetPosition(canvas1).Y);
                rec.Name = "L" + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(); 
                rec.Fill = Brushes.Black;
                canvas1.Children.Add(rec);
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            Random rd = new Random(DateTime.Now.Millisecond);
            double X = rd.Next((int)canvas1.ActualWidth);
            double Y = rd.Next((int)canvas1.ActualHeight);
            Ellipse rec = new Ellipse();
            rec.Width = 20;
            rec.Height = 20;
            rec.Name = "L" + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            rec.Fill = m_cores[rd.Next(m_cores.Length)];
            canvas1.Children.Add(rec);
            Canvas.SetLeft(rec, X);
            Canvas.SetTop(rec, Y);
            m_bolas.Add(new Bola(new Point(rd.Next(10) - 5, rd.Next(10) - 5), rec));


        }

        class Bola
        {

            public Point velo;
            public object ctr;
            public Bola(Point _velo, Shape _ctr)
            {
                velo = _velo;
                ctr = _ctr;
            }
        }

        private void ellipse1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            m_bolas.Add(new Bola(new Point(.5, .5), ellipse1));
            //ahh.Play();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            m_rotateEllipse = new RotateTransform();
            ellipse1.RenderTransform = m_rotateEllipse;
            DoubleAnimation myAnimation =
            new DoubleAnimation(
                1, // "From" value
                1200, // "To" value 
                new Duration(TimeSpan.FromSeconds(4))
            );
            myAnimation.AutoReverse = true;
            myAnimation.DecelerationRatio = .3;
            m_clock = myAnimation.CreateClock();
            m_rotateEllipse.ApplyAnimationClock(RotateTransform.AngleProperty, m_clock);
            
        }

        private void canvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TranslateTransform tt = new TranslateTransform(e.GetPosition(canvas1).X, e.GetPosition(canvas1).Y);
            ellipse1.RenderTransform = tt;

        }

        private void canvas1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            object o = e.OriginalSource;
        }


    }
}
